package com.example.hao.day8_20;

import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;


public class HttpUtils {

    private static final String TAG = "HttpUtils";

    public static String get(String address, Map<String, String> userInfo) {
        StringBuilder sb = new StringBuilder(address);
        if (!address.endsWith("?")) {
            sb.append("?");
        }
        Set<Map.Entry<String, String>> entries = userInfo.entrySet();
        for (Map.Entry<String, String> item : entries
                ) {
            sb.append(item.getKey()).append("=").append(item.getValue()).append("&");
        }
        sb.delete(sb.length() - 1, sb.length());
        Log.i(TAG, "get: " + sb.toString());
        return get(sb.toString());
    }

    public static String get(String address) {

        HttpURLConnection huc = null;
        BufferedReader br = null;
        try {
            URL url = new URL(address);
            huc = (HttpURLConnection) url.openConnection();
            huc.setConnectTimeout(5000);
            huc.setRequestMethod("GET");
            int responseCode = huc.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println("连接成功");
                br = new BufferedReader(new InputStreamReader(huc.getInputStream()));
                String read = null;
                StringBuilder sb = new StringBuilder();
                while ((read = br.readLine()) != null) {
                    sb.append(read);
                }
                return sb.toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (huc != null) {
                huc.disconnect();
            }
        }

        return null;
    }

    public static String post(String address, Map<String, String> map) {
        System.out.println("****进入post方法****");
        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for (Map.Entry<String, String> entry : entrySet
                ) {

            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        sb.delete(sb.length() - 1, sb.length());

        String content = sb.toString();
        Log.i(TAG, "***content***"+content);

        BufferedReader br = null;
        HttpURLConnection huc = null;
        BufferedWriter bw = null;
        try {
            System.out.println(address);
            URL url = new URL(address);

            huc = (HttpURLConnection) url.openConnection();

            //请求方式为 POST
            huc.setConnectTimeout(5000);
            huc.setRequestMethod("POST");
            //POST  HTTP 请求头
            huc.setRequestProperty("accept", "*/*");
            huc.setRequestProperty("connection", "Keep-Alive");
            huc.setRequestProperty("Content-Length", String.valueOf(content.getBytes().length));

            //打开输出流
            huc.setDoInput(true);
            huc.setDoOutput(true);

            //传递数据给服务器
            bw = new BufferedWriter(new OutputStreamWriter(huc.getOutputStream()));
            bw.write(content);
            bw.flush();

            int responseCode = huc.getResponseCode();
            System.out.println("***responseCode***:"+responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                Log.i(TAG, "***连接成功***");
                InputStream is = huc.getInputStream();
                br = new BufferedReader(new InputStreamReader(is));
                sb.delete(0, sb.length());
                String read ;
                while ((read = br.readLine()) != null) {
                    sb.append(read);
                }
                Log.i(TAG, "post: "+sb.toString());
                return sb.toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (huc != null) {
                huc.disconnect();
            }
        }
        return null;
    }

}